package src.com.java.dtgh.threeDay;

import java.util.Arrays;

public class demo2 {

    public int lastStoneWeightII(int[] stones){

        int sum = 0;
        for (int stone : stones) {
            sum += stone;
        }
        int target = sum >> 1;
        int[] dp = new int[target + 1];
        Arrays.fill(dp, 0);

        int len = stones.length;

        for(int i = 0;i < len;i++){
            for(int j = target;j >= stones[i];j--){
                dp[j] = Math.max(dp[j],dp[j - stones[i]] + stones[i]);
            }
        }
        return sum - dp[target] * 2;
    }
}
